package com.chzc.fm.duotin.ui.other;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.event.LoginEvent;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.widget.XEditText;

import org.greenrobot.eventbus.EventBus;

import java.util.regex.Matcher;
import java.util.regex.Pattern;



/**
 * Created by BillSinla on 2016/6/24.
 */
public class LoginDuotinActivity extends BaseActivity implements View.OnClickListener{
    //账号密码
    private XEditText editUser, editPwd;

    //
    private TextView textLogin, textNoAcc, textUse;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_duotin_login;
    }

    @Override
    protected void initViews() {
        editUser = (XEditText) findViewById(R.id.tv_login_user);
        editPwd = (XEditText) findViewById(R.id.tv_login_pwd);
        textLogin = (TextView) findViewById(R.id.btn_login);
        textNoAcc = (TextView) findViewById(R.id.tv_no_account);
        textUse = (TextView) findViewById(R.id.tv_fore_use);
    }

    @Override
    protected void initEvents() {
        textLogin.setOnClickListener(this);
        textNoAcc.setOnClickListener(this);
        textUse.setOnClickListener(this);
        editPwd.setDrawableRightListener(new XEditText.DrawableRightListener() {
            @Override
            public void onDrawableRightClick(View view) {
                startForgetPwd();
            }
        });
    }

    /**
     * 忘记密码
     */
    private void startForgetPwd() {
        Intent i = new Intent(LoginDuotinActivity.this, ForgetPwdActivity.class);
        startActivity(i);
    }

    @Override
    protected void init() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                DoLogin();
                break;
            case R.id.tv_no_account:
                startLoginActivity();
                break;
            case R.id.tv_fore_use:
                exitActivity();
                break;
        }
    }

    /**
     * 登录按钮事件处理
     */
    private void DoLogin() {
        String user = editUser.getText().toString();
        String pwd = editPwd.getText().toString();
        boolean a = isEmail(user);
        boolean b = pwd.length() >= 6;

        if(isEmail(user) && pwd.length() >= 6) {
            GlobalParam.USER_KEY = "ce2aea08904c06e5e6d435b210a8c939";
            EventBus.getDefault().post(new LoginEvent());
            exitActivity();
        }else {
            Toast.makeText(LoginDuotinActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 返回前面的界面
     */
    private void startLoginActivity() {
        Intent i = new Intent(this, LoginActivity.class);
        startActivity(i);
        exitActivity();
    }

    /**
     * 判断邮箱格式
     * @param strEmail
     * @return
     */
    public static boolean isEmail(String strEmail) {
        String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        Pattern p = Pattern.compile(strPattern);
        Matcher m = p.matcher(strEmail);
        return m.matches();
    }

    private void exitActivity() {
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startLoginActivity();
    }
}
